import axios, { AxiosRequestConfig, AxiosInstance, AxiosResponse } from 'axios'
import { message } from 'ant-design-vue'
import { t } from '@/locales'
// import { useAuthStore } from '@/store'

const service = axios.create({
  // baseURL: ""
  baseURL: process.env.NODE_ENV === 'production' ? '' : import.meta.env.VITE_GLOB_API_URL,
})

service.interceptors.request.use(
  config => {
    // const token = useAuthStore().token
    // if (token)
    //   config.headers.Authorization = `${token}`
    return config
  },
  error => {
    return Promise.reject(error.response)
  },
)

service.interceptors.response.use(
  (response: AxiosResponse): AxiosResponse => {
    const data = response.data
    if (data.success) {
      return response
    } else {
      message.error(data.error || t('common.wrong'))
      throw new Error(data)
    }
  },
  error => {
    message.error(error.message || t('common.wrong'))
    return Promise.reject(error)
  },
)

export default service
